package com.lbm.baseclass.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.lbm.baseclass.dao.IBaseDAO;
import com.lbm.baseclass.exception.ServiceException;
import com.lbm.baseclass.model.BaseEntity;
import com.lbm.baseclass.service.IBaseService;
import com.lbm.baseclass.utils.json.JsonUtil;
import com.lbm.model.ModuleInfo;
import com.lbm.model.ModuleOperate;
import com.lbm.model.RoleModule;

public class BaseServiceImpl implements IBaseService{
	protected Logger log = Logger.getLogger(this.getClass());
	protected IBaseDAO baseDAO = null;
	public void setBaseDAO(final IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	@SuppressWarnings("rawtypes")
	@Override
	public List getValueObjectsByHQL(String hql) throws ServiceException {
		return baseDAO.getValueObjectsByHQL(hql);
	}
	@Override
	public void saveOrUpdateVO(BaseEntity vo) throws ServiceException {
		baseDAO.saveOrUpdateVO(vo);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public void saveOrUpdateBatchVO(List dataList) throws ServiceException {
		baseDAO.saveOrUpdateBatchVO(dataList);
	}
	@Override
	public void deleteVO(BaseEntity vo, String id) throws ServiceException {
		baseDAO.deleteVO(vo, id);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public void deleteBatchVO(List dataList) throws ServiceException {
		baseDAO.deleteBatchVO(dataList);
	}
	@Override
	public BaseEntity getVO(BaseEntity vo, String id) throws ServiceException {
		return baseDAO.getVO(vo, id);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public List getPageDataByHQL(String hql, int page_size, int pageNo, Object... object)
			throws ServiceException {
		return baseDAO.getPageDataByHQL(hql, page_size, pageNo,object);
	}
	@Override
	public int getDataTotalNum(String hql, Object... filter)
			throws ServiceException {
		return baseDAO.getDataTotalNum(hql, filter);
	}
	@Override
	public int getTotalNumBySQL(String sql, Object... args) throws ServiceException {
		return baseDAO.getTotalNumBySQL(sql, args);
	}
	@Override
	public List<Map<String, Object>> queryForPagedListBySQL(String sql,
			int page, int rows, Object... args) throws ServiceException {
		return baseDAO.queryForPagedListBySQL(sql, page, rows, args);
	}
	@Override
	public List<Map<String, Object>> queryForListBySQL(String sql) throws ServiceException {
		return baseDAO.queryForListBySQL(sql);
	}
	public String getMenus(final String flag,final String... roleid) {				//	flag标识是否带操作权限
		return baseDAO.doInHibernate(new HibernateCallback<String>() {
			@SuppressWarnings("unchecked")
			public String doInHibernate(Session session)
					throws HibernateException, SQLException {
				String roleids="''";
				for(String temp : roleid){
					roleids+=",'"+temp+"'";
				}
				Query query = session
						.createQuery("from RoleModule where roleid in("+roleids+")");
				Query queryall = session
						.createQuery("from ModuleInfo where 1=1 order by moduleNo");
				List<RoleModule> roleModule = query.list();
				List<ModuleInfo> modules=queryall.list();
				List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
				for(ModuleInfo info : modules){
					if(info.getParentModuleid()!=null)continue;
					Map<String, Object> menu = new LinkedHashMap<String, Object>();
					eachMenu(info, menu, roleModule,flag);
					if (!menu.isEmpty())
						menus.add(menu);
				}
				return JsonUtil.toString(menus);
			}
		});
	}
	@SuppressWarnings("unchecked")
	protected void eachMenu(ModuleInfo info, Map<String, Object> menu,
			List<RoleModule> roleModule,String flag) {
		menu.put("id", info.getModuleid());
		menu.put("text", info.getModuleName());
		Map<String, String> attr = new HashMap<String, String>();
		String icon = info.getModuleIcon();
		menu.put("iconCls", "icon "+icon);
		String url = info.getAddr();
		attr.put("url", url);
		attr.put("moduleid", info.getModuleid());
		menu.put("attributes", attr);
		List<ModuleInfo> assinged=new ArrayList<ModuleInfo>();
		for(RoleModule temp : roleModule){												//取得当前用语所属角色的可操作模块
			assinged.add(temp.getModuleInfo());
		}
		if(flag==""&&assinged.contains(info))menu.put("checked", true);					
		Set<ModuleInfo> set = info.getModuleInfos();									//获取模块下的所有子模块
		List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
		if("operate".equals(flag)){
			Set<ModuleOperate> opers=info.getModuleoperates();
			if(opers.size()!=0){
				for (ModuleOperate oper : opers) {
					Map<String, Object> operMap = new HashMap<String, Object>();
					operMap.put("id", oper.getModuleOperateid());
					operMap.put("text", oper.getOperateName());
					Map<String, Object> att = new HashMap<String, Object>();
					att.put("operid", oper.getModuleOperateid());
					att.put("moduleid", info.getModuleid());
					operMap.put("attributes", att);
					for (RoleModule tp : roleModule) {
						if (oper.getModuleid().equals(tp.getModuleid())&&oper.getModuleOperateid().equals(tp.getOperateid())) {
							operMap.put("checked", true);
							break;
						}
					}
					children.add(operMap);
				}
			}else if(assinged.contains(info))menu.put("checked", true);
		}
		for (ModuleInfo child : set) {
			if(flag==""&&!assinged.contains(child))continue;
			else if (flag=="") menu.put("checked", true);
			Map<String, Object> childMenu = new LinkedHashMap<String, Object>();
			eachMenu(child, childMenu, roleModule,flag);
			children.add(childMenu);
		}
		menu.put("children", children);
	}
}
